/**
 * @author Ahammer
 * @date   2009 06
 * @update 
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;

import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.filechooser.FileNameExtensionFilter;


/** 
 * 
 * */
public class OpenTable {
	
	private static File currImgDir;


	public static void run() {
		JFileChooser fc = null;
		//JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));// im eclipse system

		try {fc = new JFileChooser();
		}
		catch (Throwable e) {
			System.out.println("OpenTable: This plugin requires Java 2 or Swing.");
			return;
		}
	
		fc.setMultiSelectionEnabled(false);
		fc.setDialogTitle("Open table");
		FileNameExtensionFilter filt     = new FileNameExtensionFilter("table files", "jtb");	 		
		fc.addChoosableFileFilter(filt);
	  
	    fc.addChoosableFileFilter(new FileNameExtensionFilter("jtb", "jtb"));
//	    fc.addChoosableFileFilter(new FileNameExtensionFilter("dat", "dat"));
//	    fc.addChoosableFileFilter(new FileNameExtensionFilter("txt", "txt"));
//	    fc.addChoosableFileFilter(new FileNameExtensionFilter("csv", "csv"));
	    fc.setFileFilter(filt);        //default setting
	    //fc.setFileFilter(filtTIFF);  //default setting
		
        currImgDir  = IqmTools.getCurrImgPath();
        //System.out.println("OpenTable: " + defaultDir.toString());
		fc.setCurrentDirectory(currImgDir);
	       
		int returnVal = fc.showOpenDialog(fc);
		if (returnVal!=JFileChooser.APPROVE_OPTION) {
			System.out.println("OpenTable: Error: No table opened");
			return;
		}	
		File file = fc.getSelectedFile();	
	
		Board.appendTexln("OpenTable: Table: ");			
		Board.appendTex(file.getName().toString()+", ");				
		Board.appendTexln(" ");			

		//String path = fc.getCurrentDirectory().getPath()+Prefs.getFileSeparator();
		//String path = fc.getCurrentDirectory().getPath() +System.getProperty("file.separator");
	    
		//set default directory
		currImgDir = fc.getCurrentDirectory();
		IqmTools.setCurrImgPath(currImgDir);
	
		//Save property to iqm.ini file
		IqmTools.setIqmIniFile("CurrImgPath", currImgDir.toString());
	    
	   JTable table = null;
	   try {
		   FileInputStream fis = new FileInputStream(file.toString());
		   ObjectInputStream ois = new ObjectInputStream(fis);
		   table = (JTable) ois.readObject();
	   } catch (FileNotFoundException e) {
		   e.printStackTrace();
	   } catch (IOException e) {
		   e.printStackTrace();
	   } catch (ClassNotFoundException e) {
		   e.printStackTrace();
	   }
	   IqmTools.displayTableData(table);		
	}

}
